﻿using GrainManage.Data.Model;
using GrainManage.Entities.Contact;
using System;
using System.Collections.Generic;

namespace GrainManage.Server.Business
{
    public class ContactBusiness : BaseBusiness, GrainManage.Interface.IContact
    {
        //************************************************POST************************************************

        public OutputSearchName SearchName(InputSearchName input)
        {
            var result = new OutputSearchName();
            try
            {
                result.Contacts = GetByColumn("ContactName", input.Name, input, input.Authentication.UserName);
                if (result.Contacts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputSearchArea SearchArea(InputSearchArea input)
        {
            var result = new OutputSearchArea();
            try
            {
                result.Areas = GetByColumn("Area", input.Area, input, input.Authentication.UserName);
                if (result.Areas.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }
            return result;
        }

        public OutputSearchAddress SearchAddress(InputSearchAddress input)
        {
            var result = new OutputSearchAddress();
            try
            {
                result.Addresses = GetByColumn("Address", input.Address, input, input.Authentication.UserName);
                if (result.Addresses.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }
            return result;
        }

        public OutputSearch Search(InputSearch input)
        {
            var result = new OutputSearch();
            try
            {
                var service = ObjectFactory.ContactService;
                result.Contacts = service.Search(input.IsOr, input.ContactFilter, input, result, input.Authentication.UserName);
                if (result.Contacts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    foreach (var item in result.Contacts)
                    {
                        if (item.ImageID.HasValue)
                        {
                            item.PhotoUrl = ImageBusiness.GetImageUrl(item.ImageID.Value, item.Creator);
                        }
                    }
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputGetByID GetByID(InputGetByID input)
        {
            var result = new OutputGetByID();
            try
            {
                var service = ObjectFactory.ContactService;
                result.Contact = service.Get(input.ContactID, input.Authentication.UserName);
                if (result.Contact == null || result.Contact.ContactID == 0)
                {
                    SetResponse(StatusCode.NoData, input, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputInsert Insert(InputInsert input)
        {
            var result = new OutputInsert();
            try
            {
                input.Contact.Creator = input.Authentication.UserName;
                var service = ObjectFactory.ContactService;
                result.ContactID = service.Insert(input.Contact);
                if (result.ContactID > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.InsertFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputUpdate Update(InputUpdate input)
        {
            var result = new OutputUpdate();
            try
            {
                input.Contact.Creator = input.Authentication.UserName;
                var service = ObjectFactory.ContactService;
                if (service.Update(input.Contact) > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.UpdateFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputDelete Delete(InputDelete input)
        {
            var result = new OutputDelete();
            try
            {
                var service = ObjectFactory.ContactService;
                if (service.Delete(input.ContactID, input.Authentication.UserName) > 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.DeleteFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchInsert BatchInsert(InputBatchInsert input)
        {
            var result = new OutputBatchInsert();
            try
            {
                var service = ObjectFactory.ContactService;
                result.ContactIDs = service.Insert(input.Contacts);
                if (!result.ContactIDs.Contains(0))
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchInsertFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchUpdate BatchUpdate(InputBatchUpdate input)
        {
            var result = new OutputBatchUpdate();
            try
            {
                var service = ObjectFactory.ContactService;
                result.InvalidContactIDs = service.Update(input.Contacts);
                if (result.InvalidContactIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchUpdateFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        public OutputBatchDelete BatchDelete(InputBatchDelete input)
        {
            var result = new OutputBatchDelete();
            try
            {
                var service = ObjectFactory.ContactService;
                result.InvalidContactIDs = service.Delete(input.ContactIDs, input.Authentication.UserName);
                if (result.InvalidContactIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, input, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchDeleteFailed, input, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, input, result, e);
            }

            return result;
        }

        private List<string> GetByColumn(string columnName, string columnFilter, IPageable page, string creator)
        {
            var service = ObjectFactory.CommonService;
            var fieldList = service.GetSpecifiedColumn("Contact", columnName, columnFilter, page, creator);
            return fieldList;
        }
    }
}
